package com.jluzh10.learning.web.controller;

import com.github.pagehelper.PageInfo;
import com.jluzh10.learning.domain.CourseClassify;
import com.jluzh10.learning.domain.CourseClassifyExample;
import com.jluzh10.learning.domain.UserExample;
import com.jluzh10.learning.service.CourseClassifyService;
import com.jluzh10.learning.utils.PinYinUtil;
import com.jluzh10.learning.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yzg
 * @create 2019/5/24
 * <p>
 * 课程归类管理
 */
@Controller
@RequestMapping("classify")
public class CourseClassifyController {

    private final CourseClassifyService classifyService;

    @Autowired
    public CourseClassifyController(CourseClassifyService classifyService) {
        this.classifyService = classifyService;
    }

    /**
     * 添加课程类
     *
     * @param classify
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData add(@Validated CourseClassify classify) throws Exception {
        //设置名称拼音
        classify.setNamePinyin(PinYinUtil.getPinYin(classify.getName()));
        //保存课程类
        int i = classifyService.addClassify(classify);
        return new ResponseData().success("添加成功").data(classify);
    }

    /**
     * 查询课程类列表
     *
     * @param size
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData list(Integer size) throws Exception {
        List<CourseClassify> classifyList = classifyService.findAll(size);
        return new ResponseData().success("查询成功").data(classifyList);
    }

    /**
     * 根据id查询课程类详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "info/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectOneById(@PathVariable(value = "id", required = false) Integer id) throws Exception {
        CourseClassify classify = classifyService.selectOneById(id);
        UserExample example=new UserExample();
        return new ResponseData().success().data(classify);
    }

}
